<?php

namespace Form\Task;

use App\Dao\Exam\ExamDao;
use App\Dao\Task\TaskHelpDao;
use App\Dao\Task\TaskTypesDao;
use Form\BaseForm;
use App\Model\Task\TaskHelpCategory;
use Yj\Apidog\Annotation\ApiController;
use Yj\Apidog\Annotation\FormData;
use Yj\Apidog\Annotation\PostApi;
use Yj\ColumnBuilder\Column;
use Yj\Form\Elm;

/**
 * @ApiController(prefix="form/task/type")
 */
class Type extends \Yj\Form\BaseForm
{
    protected $yjCode = 'task/type';
    const ADD = true;
    const EDIT = true;

    public function getColumn()
    {
        $colum = Column::create();
        $colum->setRules([
            Column::image()->setLabel('图标')->setProp('image.full_path'),
            Column::txt()->setLabel('标题')->setProp('title'),
            Column::txt()->setLabel('描述')->setProp('desc'),
            Column::txt()->setLabel('添加时间')->setProp('create_time_format'),
        ]);
        return $colum->toArray();
    }

    /**
     * @PostApi(path="config",description="配置")
     */
    public function config()
    {
        $this->_config();
    }

    /**
     * @PostApi(path="update",description="添加")
     * @FormData(key="id",rule="nullable")
     */
    public function update()
    {
        $id = $this->getParams()->get('id');
        $form = Elm::createForm($this->getRoute($id ? 'edit' : 'add'));
        $form->setRule([
            Elm::YjUpload()->field('image_id')->title('图标'),
            Elm::input('title', '标题')->required(),
            Elm::textarea('desc', '内容')->required(),
        ]);
        $id && $form->append(
            Elm::hidden('id', $id)
        );
        $lists = $form->setTitle($id ? '编辑' : '添加' . '任务帮助')->formData(function () use ($id) {
            $formData = [];
            if ($id) {
                $formData = App(TaskTypesDao::class)->where('id', $id)->first();
            }
            return $formData;
        });
        _SUCCESS(formToData($lists));
    }
}